# -*- coding: utf-8-*-
import datetime
from openerp import models
from openerp.osv import osv, fields, orm
"""
1.新建市区，和县区表方便在呼叫中心填写订单时地址的填写
"""


class res_country_state(osv.osv):
    _inherit = 'res.country.state'

    _columns = {
        'city_ids': fields.one2many('all.city', 'province_id', u'城市'),
    }


class all_city(osv.osv):
    _name = 'all.city'
    _rec_name = 'city_name'
    _columns = {
        'city_name': fields.char(u'城市名称', size=64, required=True),
        'country_ids': fields.one2many('all.county', 'city_id', u'县'),
        'province_id': fields.many2one('res.country.state', u'省名称', domain="[('country_id.name','=','中国')]", required=True),
    }
    _defaults = {
        #'province_id':lambda self, cr, uid, c:c.get('province',1),
    }


class all_county(osv.osv):
    _name = 'all.county'
    _rec_name = 'county_name'
    _columns = {
        'city_id': fields.many2one('all.city', u'城市名称', required=True),
        'county_name': fields.char(u'县名称', size=64, required=True),
        'description': fields.char(u'描述', size=64)
    }


class china_city_county(osv.osv):
    _name = 'china.city.county'

    def onchange_province(self, cr, uid, ids, county, city, province, context=None):
        # 为地址填写时方便，当选定省时 ，市区的列表里面只有所选省的
        if province:
            if city:
                city_obj = self.pool.get('all.city').browse(
                    cr, uid, [city], context=context)
                if city_obj[0].province_id.id == province:
                    if county:
                        county_obj = self.pool.get('all.county').browse(
                            cr, uid, [county], context=context)
                        if county_obj.city_id.id == city:
                            return{}
                    return {'domain': {'city_id': [('province_id', '=', province)]}, 'value': {'county_id': ''}}
                else:
                    return {'domain': {'city_id': [('province_id', '=', province)]}, 'value': {'city_id': ''}}
            else:
                return {'domain': {'city_id': [('province_id', '=', province)]}}
        else:
            return {'domain': {'city_id': [], 'county_id': []}, 'value': {'county_id': '', 'city_id': ''}}

    def onchange_county(self, cr, uid, ids, county, city, province, context=None):
        # 选定了一个区县，自动填充其所属的省和市
        if county:
            try:
                county_obj = self.pool.get('all.county').browse(
                    cr, uid, [county], context=context)
                city_obj = self.pool.get('all.city').browse(
                    cr, uid, county_obj[0].city_id.id, context=context)
                return {'domain': {'county_id': [('city_id', '=', city)]}, 'value': {'city_id': county_obj[0].city_id.id, 'province_id': city_obj[0].province_id.id}}
            except Exception, ex:
                raise osv.except_osv(u'错误', u"无法根据所选区县填充省和市")

    def onchange_city(self, cr, uid, ids, county, city, province, context=None):
        # 为地址填写时方便，当选定市时 ，县区的列表里面只有所选市的
        if city:
            city_obj = self.pool.get('all.city').browse(
                cr, uid, [city], context=context)
            province_id = city_obj[0].province_id
            if not province:
                if county:
                    county_obj = self.pool.get('all.county').browse(
                        cr, uid, [county], context=context)
                    if county_obj[0].city_id.id == city:
                        return {'domain': {'county_id': [('city_id', '=', city)]}}
                    return {'value': {'county_id': "", 'province_id': province_id.id}, 'domain': {'county_id': [('city_id', '=', city)]}}
                return {'value': {'province_id': province_id.id}, 'domain': {'county_id': [('city_id', '=', city)]}}
            else:
                if county:
                    county_obj = self.pool.get('all.county').browse(
                        cr, uid, [county], context=context)
                    if county_obj.city_id.id == city:
                        if province_id.id != province:
                            return {'domain': {'county_id': [('city_id', '=', city)], 'city_id': [('province_id', '=', province_id.id)]}, 'value': {'province_id': province_id}}
                        return {'domain': {'county_id': [('city_id', '=', city)], 'city_id': [('province_id', '=', province_id.id)]}}
                    else:
                        if province_id.id != province:
                            return {'domain': {'county_id': [('city_id', '=', city)], 'city_id': [('province_id', '=', province_id.id)]}, 'value': {'province_id': province_id.id, 'county_id': ""}}
                        return {'domain': {'county_id': [('city_id', '=', city)], 'city_id': [('province_id', '=', province_id.id)]}, 'value': {'county_id': ""}}
                else:
                    if province_id.id != province:
                        return {'domain': {'county_id': [('city_id', '=', city)], 'city_id': [('province_id', '=', province_id.id)]}, 'value': {'province_id': province_id.id}}
                    return {'domain': {'county_id': [('city_id', '=', city)], 'city_id': [('province_id', '=', province_id.id)]}}
        else:
            return {'domain': {'county_id': []}, 'value': {'county_id': ""}}

    _columns = {
        'city_id': fields.many2one('all.city', u'市'),
        'county_id': fields.many2one('all.county', u'县'),
        'province_id': fields.many2one('res.country.state', u'省', domain="[('country_id.name','=','中国')]"),
    }


class res_partner(osv.osv):
    _name = 'res.partner'
    _inherit = ['res.partner', 'china.city.county']
